package TUILayer;
import java.util.Scanner;
import CtrLayer.*;
import ModelLayer.*;
import java.util.ArrayList;

/**
 * ItemUI is the class responsible for handling the TUI of the item UI.
 * 
 * @author Gruppe 3 
 * @version 21-12-2011
 */
public class ItemUI
{
    private ItemCtr itemCtr;

    /**
     * Constructs an object of the class ItemUI and creates and initialize a itemCtr.
     */

    public ItemUI()
    {
        itemCtr = new ItemCtr();
    }

    /**
     * Starts the itemMenu.
     */

    public void start()
    {
        itemMenu();
    }

    /**
     * Writes the item menu.
     */

    private int writeItemMenu()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int choice = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Vare Menu ***");
            System.out.println(" (1) Indrapport vare ");
            System.out.println(" (2) Gå tilbage til hoved menu ");
            System.out.print("\n Tast dit valg: ");
            try
            {
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                printCatch();
                returnToMenu();
            }
        }
        return choice;
    }

    /**
     * Prints Item Menu to terminalwindow and accepts input (choices).
     */

    private void itemMenu()
    {
        boolean exit = false;
        while(!exit)
        {

            int choice = writeItemMenu();

            if (choice == 1)
            {
                createItem();
            }
            else if (choice == 2)
            {
                //mainUI.start();
                exit = true;
            }
        }
    }

    private void createItem()
    {
        try
        {
            String barCode = inputBarCode();
            String itemName = inputItemName();
            String description = inputDescription();
            double retailPrice = inputRetailPrice();
            double buyingPrice = inputBuyingPrice();
            String location = inputLocation();
            double discount = inputDiscount();
            int minAmount = inputMinAmount();
            int restockAmount = inputRestockAmount();
            int amount = inputAmount();
            itemCtr.createItem(barCode, itemName, description, retailPrice, buyingPrice, location, discount, minAmount, restockAmount, amount);
        }
        catch(Exception e)
        {
            System.out.println("");
            System.out.println("Fejl opstod");
            returnToMenu();
        }
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return barCode, the barCode of the item.
     */

    private String inputBarCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast stregkode: ");
        String barCode = keyboard.nextLine();
        return barCode;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return itemName, the name of the item.
     */

    private String inputItemName()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast vare navn: ");
        String itemName = keyboard.nextLine();
        return itemName;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return description, the description of the item.
     */

    private String inputDescription()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast beskrivelse: ");
        String description = keyboard.nextLine();
        return description;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return retailPrice, the retail price of the item.
     */

    private double inputRetailPrice()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast salgs pris i kr: ");
        double retailPrice = keyboard.nextDouble();
        return retailPrice;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return buyingPrice, the price the item cost at the supplier. 
     */

    private double inputBuyingPrice()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast købs pris i kr: ");
        double buyingPrice = keyboard.nextDouble();
        return buyingPrice;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return location, the location of the item in the warehouse.
     */

    private String inputLocation()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast lager position: ");
        String location = keyboard.nextLine();
        return location;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return discount, the discount on the item.
     */

    private double inputDiscount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast discount %: ");
        double discount = keyboard.nextDouble();
        return discount;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return minAmount, the minimum amount of the item should reach before restock.
     */

    private int inputMinAmount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast minimum beholdning på lageret: ");
        int minAmount = keyboard.nextInt();
        return minAmount;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return restockAmount, the amount the item should be restocked to if minAmount reached. 
     */

    private int inputRestockAmount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast beholdning der skal fyldes op til når minimum beholdning rammes: ");
        int restockAmount = keyboard.nextInt();
        return restockAmount;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return amount, the number of the specific item in stock.
     */

    private int inputAmount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast beholdning på lager: ");
        int amount = keyboard.nextInt();
        return amount;
    }

    /**
     * Prints out acommand that you should type in a number.
     */

    private void printCatch()
    {
        System.out.println("\n Du skal indtaste et tal - prøv igen!");
    }

    /**
     * Writes that you should type retur to return to the menu.
     * 
     * @return input, your answer to the print. 
     */

    private String returnToMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Tryk på vilkårlig tast for at komme tilbage til vare menu.");
        String input = keyboard.nextLine();
        return input;
    }

}
